import 'package:flutter/material.dart';
import 'package:my_flutter_core/services/navigator_helper.dart';
import 'package:my_flutter_core/validators/form_text_field_validator.dart';
import 'package:my_flutter_core/widgets/simple_detail_list_tile.dart';
import 'package:my_flutter_core/widgets/single_input_field.dart';

/// A simple [ListTile] to display text detail. It contains a [title] on the left, [value] on the right.
/// [valueDescription] is formatted text for [value]. Only [value] will be passed to [SingleInputField] for editing.
///
/// When taps on the tile will go to the single editing page. The updated value passes out via [onConfirmed].
/// If the editing is not needed just need to provide [onTap] method.
///
/// The [isRequired], [minLength], [maxLength] are for editing only.
class SimpleTextDetailListTile extends StatelessWidget {
  const SimpleTextDetailListTile({
    super.key,
    required this.title,
    required this.value,
    required this.formTextFieldValidator,
    this.maxLines = 1,
    this.valueDescription,
    this.keyboardType,
    this.onTap,
    this.onConfirmed,
  }) : assert(onTap != null || onConfirmed != null);

  final String title;
  final String value;
  final FormTextFieldValidator formTextFieldValidator;
  final int? maxLines;

  final TextInputType? keyboardType;
  final String? valueDescription;
  final GestureTapCallback? onTap;
  final ValueChanged? onConfirmed;

  @override
  Widget build(BuildContext context) {
    return SimpleDetailListTile(
      title: Text(title),
      value: Text(valueDescription ?? value),
      onTap:
          onTap ??
          () => NavigatorHelper.push(
            context,
            SingleInputField(
              title: title,
              initialValue: value,
              maxLines: maxLines,
              keyboardType: keyboardType,
              formTextFieldValidator: formTextFieldValidator,
              onConfirmed: onConfirmed!,
            ),
          ),
    );
  }
}
